package net.mikoo.seals.api.security;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;

import net.mikoo.seals.api.utility.ApiConstants;
import net.mikoo.seals.model.Party;

public class LocalTokenServices extends DefaultTokenServices{

	@Autowired
	private RedisTemplate<String, Object> objectRedisTemplate;
	
	@Override
	public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException {
		CustomUserDetails customUserDetails = (CustomUserDetails)authentication.getPrincipal();
		Party party = customUserDetails.getParty();
		OAuth2AccessToken token = super.createAccessToken(authentication);
		objectRedisTemplate.opsForValue().set(
				ApiConstants.MPI_OAUTH_TOKEN_TAG + token.getValue(), party, token.getExpiresIn(), TimeUnit.MILLISECONDS);
		return token;
	}	
}
